﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Realize;

namespace WebApplication9.Controllers.Permissions_Manage
{
    public class Role_PermissionsController : Controller
    {
        private readonly IConfiguration configuration;
        public Role_PermissionsController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        public IActionResult Index()//角色权限管理
        {
            return View();
        }

        public async Task<IActionResult> Select(int CurrentPage, int PageSize)
        {
            IR r = new R();
            List<Roles> zhi = await r.select(configuration["SqlServer"]);
            List<Roles> zhi2 = zhi.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
            PagedViewModel<Roles> cc = new PagedViewModel<Roles>() { itemas = zhi2, total = zhi.Count };
            return Ok(new
            {
                list = cc
            });
        }

        public IActionResult Add()//添加页面
        {
            return View();
        }



        public IActionResult Upd(string id)//修改页面
        {
            ViewBag.TitleId = id;
            return View();
        }

        public async Task<IActionResult> Delete(string id)
        {
            IR r = new R();
            int result = await r.Delete(id, configuration["SqlServer"]);
            if(result > 0)
            {
                return Ok(new
                {
                    zhi="ok"
                });
            }
            else
            {
                return Ok(new
                {
                    zhi = "no"
                });
            }
        }

        public async Task<IActionResult> AddR(string name,string sm,string zt)
        {
            IR r = new R();
            int result = await r.Add(name,sm,zt, configuration["SqlServer"]);
            if(result > 0)
            {
                return Ok(new
                {
                    zhi = "ok"
                });
            }
            else
            {
                return Ok(new
                {
                    zhi = "no"
                });
            }
        }

        public async Task<IActionResult> selectById(string id)
        {
            IR r = new R();
            Roles roles = await r.selectById(id, configuration["SqlServer"]);
            return Ok(new
            {
                zhi = roles
            });
        }


        public class RolePermissionDto
        {
            public string id { get; set; }
            public List<int> list { get; set; }
        }

        // 修改Add_RJ方法
        public async Task<IActionResult> Add_RJ([FromBody] RolePermissionDto model)
        {
            if (model == null || model.list == null || !ModelState.IsValid)
            {
                return BadRequest("无效的请求数据");
            }

            IRJ rJ = new RJ();
            int result = await rJ.Add_RJ(model.list, model.id, configuration["SqlServer"]);
            if (result > 0)
            {
                return Ok(new
                {
                    zhi = "ok"
                });
            }
            else
            {
                return Ok(new
                {
                    zhi = "no"
                });
            }
        }

        public async Task<IActionResult> getCheckedJuris(string id)
        {
            // 假设使用 Dapper 等 ORM 操作数据库，需先引用相关包
            using (var connection = new SqlConnection(configuration["SqlServer"]))
            {
                await connection.OpenAsync();
                string sql = "SELECT JuriID FROM dbo.RolesJurisdiction WHERE RolesID = @RolesID";
                var result = await connection.QueryAsync<int>(sql, new { RolesID = id });
                return Ok(new
                {
                    zhi = result.ToList()
                });
            }
        }
    }
}
